package com.example.ivideo_2211a.search

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.ivideo_2211a.R
import com.example.ivideo_2211a.databinding.ItemDetailVideoBinding


class SearchDetailVideoAdapter(private val videoList: List<SearchDataEntity>) :
    RecyclerView.Adapter<SearchDetailVideoAdapter.ItemViewHolder>() {

    private lateinit var onItemClickListener: OnItemClickListener

    interface OnItemClickListener {
        fun onItemClick( data: SearchDataEntity)
    }

    class ItemViewHolder(
        val binding: ItemDetailVideoBinding,
        private val onVideoClick: OnItemClickListener
    ) : RecyclerView.ViewHolder(binding.root) {

        fun bind(video: SearchDataEntity) {
            binding.videoDetail = video
            binding.executePendingBindings()// 执行挂起的绑定
            binding.root.setOnClickListener {
                onVideoClick.onItemClick( video)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {

        return ItemViewHolder(
            DataBindingUtil.inflate(
                LayoutInflater.from(parent.context),
                R.layout.item_detail_video,
                parent,
                false
            ), onItemClickListener
        )
    }

    override fun getItemCount(): Int {
        Log.d("TAG", "getItemCount: 搜索视频数据大小为:${videoList.size}")
        return videoList.size
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.bind(videoList[position])
    }
    fun setOnItemClickListener(listener: OnItemClickListener) {
        onItemClickListener = listener
    }


}